﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using AForge.Imaging.Filters;
using System.Threading;
using RoverStudio.Common.PositionSource;

namespace Imaging
{
    public partial class FilterChainConfigUC : UserControl, IVideoSourceCommon
    {
        private List<IFilterConfig> filters = new List<IFilterConfig>();

        public FilterChainConfigUC()
        {
            InitializeComponent();

            //filters.Add(new HSBColorFilterUC());
            //filters.Add(new ColorFilterUC());
            //filters.Add(new GrayscaleFilterUC());
            //filters.Add(new ThresholdFilterUC());
            //cbFilters.Items.Add(new HSBColorFilterUC());
            //cbFilters.Items.Add(new ColorFilterUC());
            //cbFilters.Items.Add(new GrayscaleFilterUC());
            //cbFilters.Items.Add(new ThresholdFilterUC());

            cbFilters.DisplayMember = "FilterName";

            lbFilters.DataSource = this.filters;
            lbFilters.DisplayMember = "FilterName";
            //clbFilters.DataBindings.Add("Text", filters, "FilterName");
            //clbFilters.DataSource = this.filters;
            //clbFilters.DisplayMember = "FilterName";
            //clbFilters.Refresh();
             
            
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            IFilterConfig selectedFilter = cbFilters.SelectedItem as IFilterConfig;
            if (selectedFilter == null)
                return;
            IFilterConfig newFilter = (IFilterConfig)Activator.CreateInstance( selectedFilter.GetType());

            this.filters.Add(newFilter);
            ((CurrencyManager)this.lbFilters.BindingContext[filters]).Refresh();
           this.splitContainer.Panel2.Controls.Clear();
            this.splitContainer.Panel2.Controls.Add( newFilter.FilterGUI);
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            IFilterConfig selectedFilter = lbFilters.SelectedItem as IFilterConfig;
            this.filters.Remove(selectedFilter);
            ((CurrencyManager)this.lbFilters.BindingContext[filters]).Refresh();
            this.splitContainer.Panel2.Controls.Clear();

        }


        private void lbFilters_SelectedIndexChanged(object sender, EventArgs e)
        {
            IFilterConfig selectedFilter = lbFilters.SelectedItem as IFilterConfig;
            this.splitContainer.Panel2.Controls.Clear();
            this.splitContainer.Panel2.Controls.Add(selectedFilter.FilterGUI);

        }

        public void FilterBitmap(Bitmap bitmap)
        {
            Bitmap filteredImage = bitmap;
            foreach (IFilterConfig filterConfig in this.filters)
            {
                if (filterConfig.Filter == null)
                    continue;
                try
                {
                    filteredImage = filterConfig.Filter.Apply(filteredImage);
                }
                catch
                {
                    continue;
                }
            }
            try
            {
                if (NewFrame != null)
                    NewFrame(filteredImage);
            }
            catch
            {

            }

            pbFilteredImage.Invoke(new ThreadStart(delegate()
            {
                pbFilteredImage.Image = filteredImage;
            }));
            //pbFilteredImage.Image = bitmap;
        }


        #region IVideoSourceCommon Members

        public event NewFrameDelegate NewFrame;

        public bool IsActive
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }
}
